<?php
require dirname(__DIR__) . '/vendor/autoload.php';

use Yurun\Util\HttpRequest;
use Yurun\Util\YurunHttp;


// YurunHttp::setDefaultHandler(\Yurun\Util\YurunHttp\Handler\Swoole::class);

       go(function(){
            $url = 'ws://3d.he4966.cn:3333/ws';
            $http = new HttpRequest;
            $client = $http->websocket($url);
            if(!$client->isConnected())
            {
                throw new \RuntimeException('Connect failed');
            }
            $client->send('{"action":"isupdate","group":"twk3","message":"2021050572"}');
            $recv = $client->recv();
            var_dump('recv:', $recv);


            $i = 10 ;
            while($i>0){
                $client->send('{"action":"isupdate","group":"twk3","message":"2021090712"}');
                
                $recv = $client->recv();
                var_dump('recv:', $recv);
                sleep(3);
                $i--;
            }
            $client->close();
        });





// YurunHttp::setDefaultHandler(\Yurun\Util\YurunHttp\Handler\Swoole::class);
// go(function () {
//     // 该测试地址随时可能过期
//     $url = 'wss://et65.71at.cn:5000/';//'ws://3d.he4966.cn:3333/ws';
//     $http = new HttpRequest();
//     $http->header('Origin', 'http://3d.he4966.cn');
//     $client = $http->websocket($url);
//     if (!$client->isConnected())
//     {
//         throw new \RuntimeException('Connect failed');
//     }
//     // $time = time() . '';
//     // var_dump('time:', $time);
//     $time = '{"action":"isupdate","group":"twk3","message":"2021050572"}';
//     $time = StrToBin('123');
//     echo $time;
//     // $bug=true;
//     $i = 10 ;
//     while($i>0){
//         $client->send($time);
        
//         $recv = $client->recv();
//         var_dump('recv:', $recv);
//         sleep(1);
//         $i--;
//     }

//     $client->close();
// });




function StrToBin($str){

    //1.列出每个字符

    $arr = preg_split('/(?<!^)(?!$)/u',$str);
    //2.unpack字符

    foreach($arr as &$v){

        $temp = unpack('H*', $v);

        $v = base_convert($temp[1], 16, 2);

        unset($temp);

    }

    return join(' ',$arr);

}

// use Yurun\Util\HttpRequest;

// $http = new HttpRequest;

// // 设置 Header 4 种方法
// $http->header('aaa', 'value1')
//      ->headers([
//          'bbb' => 'value2',
//          'ccc' => 'value3',
//      ])
//      ->rawHeader('ddd:value4')
//      ->rawHeaders([
//          'eee:value5',
//          'fff:value6',
//      ]);

// // 请求
// $response = $http->ua('YurunHttp')
//                  ->get('http://www.baidu.com');

// echo 'html:', PHP_EOL, $response->body();

?>